/*****************************************************************************
* File Name: ble.h
*
* Version: 1.0
*
* Description:
* This file contains API for sending HID reports over BLE
*
* Note:
* Defining DISABLE_BLE in platform.h to disable ble module
*
* Owner: MRAO
* 
* Related Document:
* BLE Component datasheet
*
* Hardware Dependency:
* Antenna 
*
* Code Tested With:
* 1. PSoC Creator 3.1 
* 2. ARM GCC 4.8.4
******************************************************************************
* Copyright (2015), Cypress Semiconductor Corporation.
******************************************************************************
* This software is owned by Cypress Semiconductor Corporation (Cypress) and is
* protected by and subject to worldwide patent protection (United States and
* foreign), United States copyright laws and international treaty provisions.
* Cypress hereby grants to licensee a personal, non-exclusive, non-transferable
* license to copy, use, modify, create derivative works of, and compile the
* Cypress Source Code and derivative works for the sole purpose of creating
* custom software in support of licensee product to be used only in conjunction
* with a Cypress integrated circuit as specified in the applicable agreement.
* Any reproduction, modification, translation, compilation, or representation of
* this software except as specified above is prohibited without the express
* written permission of Cypress.
*
* Disclaimer: CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH
* REGARD TO THIS MATERIAL, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 
* Cypress reserves the right to make changes without further notice to the
* materials described herein. Cypress does not assume any liability arising out
* of the application or use of any product or circuit described herein. Cypress
* does not authorize its products for use as critical components in life-support
* systems where a malfunction or failure may reasonably be expected to result in
* significant injury to the user. The inclusion of Cypress' product in a life-
* support systems application implies that the manufacturer assumes all risk of
* such use and in doing so indemnifies Cypress against all charges. Use may be
* limited by and subject to the applicable Cypress software license agreement.
*****************************************************************************/
#ifndef _BLE_H_
#define _BLE_H_

#include "platform.h"
#include "device.h"

#define BLE_ADVERTISE_STARTED                            (0)
#define BLE_ADVERTISE_ALREADY_STARTED                    (1)
#define BLE_ADVERTISE_NOT_STARTED                        (2)
#define BLE_ADVERTISE_NOT_STARTED_CONNECTED_STATE        (3)

#ifndef DISABLE_BLE

/* BLE Modules states */
#define BLE_STATE_STOPPED                               ((uint8)CYBLE_STATE_STOPPED)
#define BLE_STATE_INITIALIZING                          ((uint8)CYBLE_STATE_INITIALIZING)
#define BLE_STATE_CONNECTED                             ((uint8)CYBLE_STATE_CONNECTED)
#define BLE_STATE_DISCONNECTED                          ((uint8)CYBLE_STATE_DISCONNECTED)
#define BLE_STATE_ADVERTISING                           ((uint8)CYBLE_STATE_ADVERTISING)
#define BLE_STATE_CONNECTED_PATH_LOSS                   ((uint8)0xFF)
#define BLE_STATE_DISCONNECTED_LINK_LOSS                ((uint8)0xFE)

/* L2CAP Connection Updata parameter for various modes */
#define BLE_ACTIVE_SLAVE_LATENCY                        (100u)   /* Slave Latency */
#define BLE_IDLE_SLAVE_LATENCY                          (100u)   /* Slave Latency */
#define BLE_SLEEP_SLAVE_LATENCY                         (100u)   /* Slave Latency */

#define BLE_ACTIVE_CONNECTION_INTERVAL                  (8u)     /* 10 ms */
#define BLE_IDLE_CONNECTION_INTERVAL                    (8u)     /* 10 ms */
#define BLE_SLEEP_CONNECTION_INTERVAL                   (8u)     /* 10 ms */

#define BLE_ACTIVE_SUPERVISION_TIMEOUT                  (300u)   /* 3 sec */
#define BLE_IDLE_SUPERVISION_TIMEOUT                    (300u)   /* 3 sec */
#define BLE_SLEEP_SUPERVISION_TIMEOUT                   (300u)   /* 3 sec */

#define BLE_CONNECT_TO_ONLY_WHITE_LIST_TIMEOUT          (3u)     /* 3 sec */
#define BLE_CONNECT_TO_ANY_TIMEOUT                      (30)     /* 30 sec */

#define BLE_AUDIO_CHANNEL_CREATE_COMMAND                (0x1)
#define BLE_AUDIO_CHANNEL_DISCONNECT_COMMAND            (0x0)
#define BLE_AUDIO_CHANNEL_COMMAND_OPCODE                (0xFF)
#define BLE_AUDIO_CHANNEL_COMMAND_LENGTH                (0x02)

/* Reason for the Disconnections */
#define BLE_LINK_CONNECTION_TIMEOUT                     (0x08)   /* Connection Timeout */
#define BLE_LINK_TERMITATION_REMOTE_USER                (0x13)   /* Remote User Terminated Connection */
#define BLE_LINK_TERMITATION_LOW_RESOURCE               (0x14)   /* Remote Device Terminated Connection due to Low Resources */
#define BLE_LINK_TERMITATION_LINK_LOSS                  (0x15)   /* Remote Device Terminated Connection due to Power Off */
#define BLE_LINK_TERMITATION_LOCAL_HOST                 (0x16)   /* Connection Terminated By Local Host */

#ifdef ENABLE_BLESS_CLOCK_CONFIGURATION
#define BLE_CRYSTAL_PPM                                 DEVICE_CRYSTAL_PPM                   
#define BLE_ECO_DIVIDER                                 DEVICE_ECO_DIVER                 
#define BLE_ECO_START_UP_TIME                           DEVICE_ECO_START_UP_TIME
#endif /* ENABLE_BLESS_CLOCK_CONFIGURATION */

#ifdef ENABLE_CONNECTION_UPDATE_REJECT_DISCONNECT
#define L2CAP_UPDATE_PARAMETER_REJECT_THRESHOLD         (10u)
#endif /* ENABLE_CONNECTION_UPDATE_REJECT_DISCONNECT */
#define Ble_IsAdvertisingCompleted()                     (CyBle_GetState() != BLE_STATE_ADVERTISING)
#define Ble_DisableLowPowerMode(value)                   (disableLowPowerMode = value)
#define Ble_ExitLPM()                                     CyBle_ExitLPM()

/* This functions is used to check if the BLE initiation is complete */
#define Ble_Is_Init_Completed()                          (CyBle_GetState() != BLE_STATE_INITIALIZING)

extern bool disableLowPowerMode;

/*****************************************************************************
* Function Name: Ble_Init()
******************************************************************************
* Summary:
* This functions is performs the required BLE initialization and for various 
* profiles
*
* Parameters:
* None
*
* Return:
* None
*
*****************************************************************************/
extern void Ble_Init(void);

/*****************************************************************************
* Function Name: Ble_Configure()
******************************************************************************
* Summary:
* This function configures the Clock configration of the BLESS and puts the BLESS
* into low power mode
*
* Parameters:
* None
*
* Return:
* None
*
* Note:
* This function should be called after Ble_Is_Init_Completed() return true
*****************************************************************************/
extern void Ble_Configure(void);

/*****************************************************************************
* Function Name: Ble_IsValidPeerAddressPresent()
******************************************************************************
* Summary:
* This function checks if the last connected peer address is valid or not
*
* Parameters:
* None
*
* Return:
* bool - True if the last connected peer address is valid
*
*****************************************************************************/
extern bool Ble_IsValidPeerAddressPresent(void);

/*****************************************************************************
* Function Name: Ble_Set_Address()
******************************************************************************
* Summary:
* This function set the public BLE BD address for the device
*
* Parameters:
* uint8 *address - pointer to the ble address that need to be set. Pass NULL to
*                  generate random Public BLE BD address
*
* Return:
* None
*
* Note:
* address pointer should be at least 6 bytes
*****************************************************************************/
extern void Ble_Set_Address(uint8 *address);

/*****************************************************************************
* Function Name: Ble_Get_State()
******************************************************************************
* Summary:
* This function returns the current state of BLE
*
* Parameters:
* None
*
* Return:
* uint8 - state of the ble
*
*****************************************************************************/
extern uint8 Ble_Get_State(void);

/*****************************************************************************
* Function Name: Ble_Set_State()
******************************************************************************
* Summary:
* This function sets the device state to the BLE module
*
* Parameters:
* state - state of the device
*
* Return:
* None
*
*****************************************************************************/
extern void Ble_Set_State(Device_State state);

/*****************************************************************************
* Function Name: Ble_Create_Audio_Channel()
******************************************************************************
* Summary:
* This function is used to create an audio channel with the peer device
*
* Parameters:
* None
*
* Return:
* bool - return true if BLE connection is initiated by the BLE component
*
*****************************************************************************/
extern bool Ble_Create_Audio_Channel(void);

/*****************************************************************************
* Function Name: Ble_Disconnect_Audio_Channel()
******************************************************************************
* Summary:
* This function is used to disconnect the audio channel with the peer device
*
* Parameters:
* None
*
* Return:
* None
*
*****************************************************************************/
extern void Ble_Disconnect_Audio_Channel(void);
/*****************************************************************************
* Function Name: Ble_Force_StopAdvertisement()
******************************************************************************
* Summary:
* None
*
* Parameters:
* None
*
* Return:
* None
*
* Theory:
* None
*
* Side Effects:
* None
* 
* Note:
* None
*****************************************************************************/
extern void Ble_Force_StopAdvertisement(void);
/*****************************************************************************
* Function Name: Ble_StartAdvertisement()
******************************************************************************
* Summary:
* This function starts undirected\directed advertising over BLE to discover the
* client and connect to it
*
* Parameters:
* None
*
* Return:
* uint8 - returns status of the Advertisement
* Codes                                      Description
* ------                                     -------------
* BLE_ADVERTISE_STARTED                     - ADV has be started due this API call
* BLE_ADVERTISE_ALREADY_STARTED             - ADV was already in progress due to eariler API call
* BLE_ADVERTISE_NOT_STARTED_CONNECTED_STATE - ADV cannot be stated as it is in connected state
* BLE_ADVERTISE_NOT_STARTED                 - ADV cannot be stated due to errors
*****************************************************************************/
extern uint8 Ble_StartAdvertisement(bool startedDirectedAdvertisement, bool scannableOnlyFromWhitelist);

/*****************************************************************************
* Function Name: Ble_StopAdvertisement()
******************************************************************************
* Summary:
* This function stops advertising
*
* Parameters:
* None
*
* Return:
* None
*
*****************************************************************************/
extern void Ble_StopAdvertisement(void);

/*****************************************************************************
* Function Name: Ble_Send_Battery_Data()
******************************************************************************
* Summary:
* This function is used to send battery status to the connected client
*
* Parameters:
* batteryLevel - Battery status expressed in percentage
*
* Return:
* bool - returns true if battery status is sent over BLE
*
*****************************************************************************/
extern bool Ble_Send_Battery_Data(uint8 batteryLevel);

/*****************************************************************************
* Function Name: Ble_Send_Data()
******************************************************************************
* Summary:
* This function is used to sends report to the connected client
*
* Parameters:
* reportType - Type of the report that need to be sent
* report - Report data to be sent
*
* Return:
* bool - returns true if report is sent over BLE
*
*****************************************************************************/
extern bool Ble_Send_Data(ReportType reportType, Report *report);

/*****************************************************************************
* Function Name: Ble_Stop()
******************************************************************************
* Summary:
* This function stops the BLE module
*
* Parameters:
* None
*
* Return:
* None
*
*****************************************************************************/
extern void Ble_Stop(void);

/*****************************************************************************
* Function Name: Ble_WriteBondedList()
******************************************************************************
* Summary:
* This function writes the Bond list into the flash
*
* Parameters:
* None
*
* Return:
* None
* 
*****************************************************************************/
extern void Ble_WriteBondedList(void);

/*****************************************************************************
* Function Name: Ble_Enter_LowPowerMode()
******************************************************************************
* Summary:
* This function instructs the BLESS to enter deep sleep mode
*
* Parameters:
* None
*
* Return:
* bool - true if the BLESS enters the deep sleep mode
* 
*****************************************************************************/
extern bool Ble_Enter_LowPowerMode(void);

/*****************************************************************************
* Function Name: Ble_ProcessEvents()
******************************************************************************
* Summary:
* This function checks if any activity is pending from the stack as well from the 
* BLE module
*
* Parameters:
* None
*
* Return:
* None
*
*****************************************************************************/
extern void Ble_ProcessEvents(void);

#else
/* BLE Modules states */
#define BLE_STATE_STOPPED                               ((uint8)0)
#define BLE_STATE_INITIALIZING                          ((uint8)1)
#define BLE_STATE_CONNECTED                             ((uint8)2)
#define BLE_STATE_DISCONNECTED                          ((uint8)3)
#define BLE_STATE_ADVERTISING                           ((uint8)4)
#define BLE_STATE_CONNECTED_PATH_LOSS                   ((uint8)0xFF)
#define BLE_STATE_DISCONNECTED_LINK_LOSS                ((uint8)0xFE)

#define Ble_ProcessEvents()
#define Ble_IsAdvertisingCompleted()                    true
#define IsBleTransmitting()                             false
#define Ble_Set_Address(x)
#define Ble_Init()
#define Ble_Is_Init_Completed()                         true
#define Ble_Get_State()                                 BLE_STATE_DISCONNECTED
#define Ble_Set_State(state)
#define Ble_Create_Audio_Channel()                      true
#define Ble_Disconnect_Audio_Channel()
#define Ble_Get_Notification_Status()                   0xFF
#define Ble_StartAdvertisement(sDAdvertisement,sOFW)    BLE_ADVERTISE_NOT_STARTED
#define Ble_StopAdvertisement()
#define Ble_Send_Battery_Data(batteryLevel)             true
#define Ble_Send_Data(reportType, report)               true
#define Ble_Stop()
#define Ble_Enter_LowPowerMode()                        true
#define Ble_DisableLowPowerMode(value)
#define Ble_ExitLPM()
#define Ble_WriteBondedList()
#define Ble_IsValidPeerAddressPresent()                 false
#define Ble_Configure()
#endif /* DISABLE_BLE */

#endif /* _BLE_H_ */
